package top.carrotguo.weibo.ui.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.sina.weibo.sdk.auth.AccessTokenKeeper;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import top.carrotguo.weibo.R;
import top.carrotguo.weibo.adapter.CommentAdapter;
import top.carrotguo.weibo.adapter.EndlessRecyclerOnScrollListener;
import top.carrotguo.weibo.adapter.LoadingMoreWrapper;
import top.carrotguo.weibo.adapter.StringAdapter;
import top.carrotguo.weibo.bean.CommentListBean;
import top.carrotguo.weibo.contract.CommentContract;
import top.carrotguo.weibo.presenter.CommentPresenter;
import top.carrotguo.weibo.retrofit.callback.ErrorInfo;
import top.carrotguo.weibo.ui.base.MVPBaseFragment;

public class CommentFragment extends MVPBaseFragment<CommentContract.View,CommentPresenter>
    implements CommentContract.View{

    /*存放评论列表*/
    @BindView(R.id.rv_container)
    RecyclerView rvContainer;
    /*获取不到评论列表或者评论列表为空的展示页*/
    @BindView(R.id.ll_error)
    LinearLayout llError;
    /*加载中*/
    @BindView(R.id.pg_loading)
    ProgressBar pgLoading;
    /*获取不到评论的原因*/
    @BindView(R.id.tv_error_message)
    TextView tvErrorMessage;

    private LinearLayoutManager mManager;
    private ArrayList<CommentListBean.CommentsBean> datas = new ArrayList<>();
    private CommentAdapter mAdapter;
    private LoadingMoreWrapper mWrapper;
    private String id;
    public static final String BLOG_ID = "BLOG_ID";
    private int mCount = 10;
    private int mPage = 1;

    /**
     * 用此种方式获取fragment实例可实现解耦
     * @param id
     * @return
     */
    public static CommentFragment newInstance(String id){
        CommentFragment fragment = new CommentFragment();
        Bundle bundle = new Bundle();
        bundle.putString(BLOG_ID,id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void showCommentsSuccessed(CommentListBean successInfo) {
        //显示recyclerview
        rvContainer.setVisibility(View.VISIBLE);
        llError.setVisibility(View.GONE);

        //添加数据
        datas.addAll(successInfo.getComments());
        //更新数据
        mWrapper.notifyDataSetChanged();
        //页数+1(得到的页码是下次要请求的页码)
        mPage++;
    }

    @Override
    public void showCommentsFailed(ErrorInfo errorInfo) {
        //隐藏recyclerview
        rvContainer.setVisibility(View.GONE);
        llError.setVisibility(View.VISIBLE);
        tvErrorMessage.setText(errorInfo.getErrorMsg());
    }

    @Override
    public void showCommentsEmpty() {
        //隐藏recyclerview
        rvContainer.setVisibility(View.GONE);
        llError.setVisibility(View.VISIBLE);
        tvErrorMessage.setText("没有人评论哦~");
    }

    /**
     * 显示正在加载评论列表
     */
    @Override
    public void showLoadingComment() {
        pgLoading.setVisibility(View.VISIBLE);
        rvContainer.setVisibility(View.GONE);
        llError.setVisibility(View.GONE);
    }

    /**
     * 隐藏正在加载评论列表
     */
    @Override
    public void hideLoadingComment() {
        pgLoading.setVisibility(View.GONE);
    }

    @Override
    public void loadMoreSuccess(CommentListBean successInfo) {
        datas.addAll(successInfo.getComments());
        if (successInfo.getComments().size()==mCount) {
            //还有更多数据(此方法中已经调用了刷新数据)页数+1
            mWrapper.setLoadingStates(LoadingMoreWrapper.LOADING_TYPE_COMPLETE);
            mPage++;
        } else if (successInfo.getComments().size() > 0){
            //没有更多数据,但是当前有加载到数据，所以页数也要+1
            mWrapper.setLoadingStates(LoadingMoreWrapper.LOADING_TYPE_END);
            mPage++;
        } else {
            //加载到空数据,只是让其设置为加载结束，但是不增加页数
            mWrapper.setLoadingStates(LoadingMoreWrapper.LOADING_TYPE_END);
        }
    }

    @Override
    public void loadMoreFail(ErrorInfo errorInfo) {
        //若加载失败，则设置为加载完成，即不做任何处理
        mWrapper.setLoadingStates(LoadingMoreWrapper.LOADING_TYPE_COMPLETE);
    }

    @Override
    protected CommentPresenter createPresenter() {
        return new CommentPresenter();
    }

    @Override
    protected void getBundleExtras(Bundle bundle) {
        //获取要加载的是哪个id的微博的评论
        id = bundle.getString(BLOG_ID);
    }

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return LayoutInflater.from(mContext).inflate(R.layout.fragment_comment,container,false);
    }

    @Override
    protected void initLogic() {
        ButterKnife.bind(this,mRootView);
        initComment();
    }

    private void initComment(){
        mManager = new LinearLayoutManager(mContext);
        rvContainer.setLayoutManager(mManager);
        mAdapter = new CommentAdapter(datas);
        mWrapper = new LoadingMoreWrapper(mAdapter);
        rvContainer.setAdapter(mWrapper);
/*        rvContainer.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
            @Override
            public void onLoadMore() {
                //先更改footer的状态为加载中
                mWrapper.setLoadingStates(LoadingMoreWrapper.LOADING_TYPE_LOADING);
                //加载更多
                Logger.e("加载更多");
                mPresenter.getComments(AccessTokenKeeper.readAccessToken(mContext),
                        id,
                        mCount,
                        mPage);
            }
        });*/



        //加载第一页数据
        mPresenter.getComments(AccessTokenKeeper.readAccessToken(mContext),id,mCount,mPage);
    }

    public void onLoadMore(){
        //先更改footer的状态为加载中
        mWrapper.setLoadingStates(LoadingMoreWrapper.LOADING_TYPE_LOADING);
        //加载更多
        mPresenter.getComments(AccessTokenKeeper.readAccessToken(mContext),
                id,
                mCount,
                mPage);
    }

    public void refreshList(){
        datas.clear();
        mPage = 1;
        //加载第一页数据
        mPresenter.getComments(AccessTokenKeeper.readAccessToken(mContext),id,mCount,mPage);
    }



}
